package Hot100;

import LinkedList.ListNode;

public class 两两交换链表中的节点 {
    public ListNode swapPairs(ListNode head) {
        // 迭代
        /*if (head == null || head.next == null) return head;
        ListNode curr = head;
        ListNode newHead = head.next;
        ListNode aftr = newHead;
        while (curr != aftr) {
            curr.next = aftr.next;
            aftr.next = curr;
            ListNode temp = curr;
            curr = curr.next;
            if (temp.next != null && temp.next.next != null) temp.next = temp.next.next;
            aftr = temp.next;
        }
        return newHead;*/

        // 递归
        if (head == null || head.next == null) return head;
        // 获取当前节点的下一个节点
        ListNode next = head.next;
        // 进行递归
        ListNode newNode = swapPairs(next.next);
        // 这里进行交换
        next.next = head;
        head.next = newNode;
        return next;
    }
}
